// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//导入elementui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import store from '@/store';
import axios from 'axios';

Vue.use(ElementUI);
Vue.config.productionTip = false

//配置axios
Vue.prototype.$http = axios //修改内部的$http为axios
axios.defaults.baseURL = 'http://localhost:8086/manage';
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  config.headers['Authorization'] = sessionStorage.getItem('token');

  // 在发送请求之前做些什么
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

/**
 * 使用router钩子函数处理
 */
router.beforeEach((to, from, next) => {
  
  const token = sessionStorage.getItem('token')
  if (to.name !== 'login' && !token) next({ name: 'login' })
  // 如果用户未能验证身份，则 `next` 会被调用两次
  else next()
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,//挂载Router实例
  store,//挂载store实例
  components: { App },
  template: '<App/>'
})
